import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../bean/opetation_detail.dart';
import '../../../../res/HDColors.dart';
import '../../../../view/FieldEditor.dart';
import '../../../../view/common_widget.dart';
import 'logic.dart';

class AddOperationPage extends StatelessWidget {
  final logic = Get.put(AddOperationLogic());
  final state = Get.find<AddOperationLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: const Text("新增运维"),
            elevation: 0,
            backgroundColor: HDColors.green,
            leading: CommonWidget.getAppBarLeading()),
        body: SingleChildScrollView(
          physics: ClampingScrollPhysics(),
          child: Column(
            children: [
              Container(
                height: 30,
              ),
              Autocomplete<OperationBean>(
                optionsBuilder: buildOptions,
                fieldViewBuilder: _buildFieldView,
                displayStringForOption: (OperationBean option) =>
                    option.customerName!,
                onSelected: (value) {
                  state.operationBean = value;
                  logic.update();
                },
              ),
              GetBuilder<AddOperationLogic>(builder: (logic) {
                return Stack(children: [
                  Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          height: 5,
                        ),
                        FieldEditor(
                            label: '客户电话:',
                            hintText: '请输入客户手机号',
                            contentTextAlign: TextAlign.end,
                            showLine: false,
                            isRequired: false,
                            readOnly: true,
                            initialValue: state.operationBean.phoneNumber),
                        FieldEditor(
                          label: '建站地址:',
                          hintText: '请输入建站地址',
                          contentTextAlign: TextAlign.end,
                          showLine: false,
                          isRequired: false,
                          readOnly: true,
                          initialValue: state.operationBean.buildingAddress,
                        ),
                        FieldEditor(
                          label: '踏勘位置:',
                          hintText: '请输入踏勘位置',
                          contentTextAlign: TextAlign.end,
                          showLine: false,
                          isRequired: false,
                          readOnly: true,
                          initialValue: state.operationBean.address,
                        ),
                        FieldEditor(
                          label: '故障说明:',
                          hintText: '请在下方填写故障说明',
                          contentTextAlign: TextAlign.end,
                          showLine: false,
                          isRequired: false,
                        ),
                        BrnInputText(
                          maxHeight: 500,
                          minHeight: 40,
                          maxLength: 500,
                          autoFocus: false,
                          bgColor: Colors.grey[200]!,
                          textInputAction: TextInputAction.newline,
                          maxHintLines: 200,
                          hint: state.operationBean.faultDescription ?? '请描述故障说明',
                          padding: EdgeInsets.fromLTRB(15, 10, 20, 14),
                          onTextChange: (text) {
                            state.operationBean.faultDescription = text;
                          },
                        )
                      ]),
                ]);
              }),
              getFooterButton()
            ],
          ),
        ));
  }

  Future<Iterable<OperationBean>> buildOptions(
      TextEditingValue textEditingValue) async {
    if (textEditingValue.text == '') {
      return const Iterable<OperationBean>.empty();
    }
    return searchByArgs(textEditingValue.text);
  }

  Future<Iterable<OperationBean>> searchByArgs(String args) async {
    print("args is ${args}");
    state.dataList = await logic.requestList(searchKey: args);
    return state.dataList.where(
        (OperationBean tempBean) => tempBean.customerName!.contains(args));
  }

  Widget _buildFieldView(
      BuildContext context,
      TextEditingController textEditingController,
      FocusNode focusNode,
      VoidCallback onFieldSubmitted) {
    return Container(
      margin: const EdgeInsets.only(left: 15, right: 15),
      height: 40,
      child: Row(
        children: [
          Text(
            "客户姓名:",
            style: TextStyle(fontSize: 16),
          ),
          const SizedBox(
            width: 10,
          ),
          Expanded(
              child: TextFormField(
            controller: textEditingController,
            focusNode: focusNode,
            decoration: const InputDecoration(
              hintText: "输入用户姓名，自动填充信息",
              border: OutlineInputBorder(
                borderSide: BorderSide(color: Colors.black12, width: 1.0),
              ),
            ),
            onFieldSubmitted: (value) {
              onFieldSubmitted();
            },
          ))
        ],
      ),
    );
  }

  getFooterButton() {
    return Container(
        margin: const EdgeInsets.only(top: 300),
        padding: const EdgeInsets.symmetric(horizontal: 0, vertical: 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            CommonWidget.buildBottomBtn("新增运维", () {
              logic.submit();
            }),
          ],
        ));
  }
}
